package com.ioidea.ble.proto

import com.ioidea.base.util.MyLogger
import com.ioidea.base.util.Symbol
import java.lang.Exception

/**
 * 3.5 PLC全控 SETDIMS
数据： 500 600 700
示例：手机->设备
SETDIMS 500 600 700
示例：设备->手机
SETDIMS 1
 */
class ProtoAllLightControl(
    override var requestCommand: String = "SETDIMS",//"SETDIMS",
    override var responseCommand: String = "SETDIMS"
) : ProtoBase() {


    /**
     *  亮度均为百分比值  群控需要转成具体数值  1000x50%=500
     * @param brightness1
     * @param brightness2
     * @param brightness3 暂时不用 默认0
     */
    fun getPayload(
        brightness1: Int,
        brightness2: Int,
        brightness3: Int = 0
    ): String {
        val brightness1Str = (10 * brightness1).toString()
        val brightness2Str = (10 * brightness2).toString()
        val brightness3Str = (10 * brightness3).toString()
        val payLoadBuilder = StringBuilder()
        payLoadBuilder.append(brightness1Str).append(Symbol.SPACE).append(brightness2Str)
            .append(Symbol.SPACE)
            .append(brightness3Str)
        return payLoadBuilder.toString()
    }

    fun parseAllControlResponse(wholeData: String): Int {
        //去头尾
        val data = removeHeadTail(wholeData)
        val head = "$HEAD_RESPONSE$sn${Symbol.MAO_HAO}$responseCommand"
        MyLogger.hLog().i("data:$data")
        if (wholeData.contains(head)) {
            try {
                return data.toInt()
            } catch (e: Exception) {
                MyLogger.hLog().e(e.message)
            }
            return -1
        }
        return 0
    }
}